package virtualstock.game;

import java.io.IOException;
import java.io.PrintWriter;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import virtualstock.exception.GameNotFoundException;
import virtualstock.exception.InvalidParameterException;
import virtualstock.request.GameController;

/**
 * Servlet implementation class GameRemoveServlet
 */
public class GameRemoveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@EJB
	private GameController gameController;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GameRemoveServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	PrintWriter out = response.getWriter();
    	String gameIDStr = request.getParameter("gameID");
    	if (gameIDStr == null) {
    		out.println("<meta http-equiv=\"refresh\" content=\"1;url=adminHomePage.jsp\">");
    		out.println("No game selected. <br> automated redirect in 1 second");
    		return;
    	}
    	Long gameID = new Long(gameIDStr);
    	
    	try{
    		gameController.removeGame(gameID);
    	}catch( InvalidParameterException e){
    		out.println(e.getMessage());
    		e.printStackTrace();
    	}catch(GameNotFoundException e){
    		out.println(e.getMessage());
    		e.printStackTrace();
    	}catch(Exception e){
    		e.printStackTrace();
    	}
    	
    	System.out.println("+++++GameRemoveServlet: before Redirection!");
    	response.sendRedirect("adminHomePage.jsp");
    }
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

}
